﻿using System;
using élénkPróbája.validators.adaptors;

namespace élénkPróbája.native.rule
{
    public class LitPropTestAdmin<T, TProperty> : ITestAdminOptions<T, TProperty>, ITestAdminInitial<T, TProperty>
    {
        PropRule rule;

        public PropRule Rule
        {
            get { return rule; }
        }

        public LitPropTestAdmin(PropRule rule)
        {
            if (rule != null) this.rule = rule;
        }

        public ITestAdminOptions<T, TProperty> SetValidator(ILitPropertyValidator validator)
        {
            validator.Guard("Cannot pass a null validator to SetValidator.");
            rule.AddValidator(validator);
            return this;
        }
        public ITestAdminOptions<T, TProperty> SetValidator(IValidator<TProperty> validator)
        {
            validator.Guard("Cannot pass a null validator to SetValidator");
            SetValidator(new SubValidatorAdaptor(validator));
            return this;
        }
        public ITestAdminOptions<T, TProperty> SetValidator<TValidator>(Func<T, TValidator> validatorProvider)
    where TValidator : IValidator<TProperty>
        {
            validatorProvider.Guard("Cannot pass a null validatorProvider to SetValidator");
            SetValidator(new SubValidatorAdaptor(t => validatorProvider((T)t), typeof(TProperty)));
            return this;
        }
        ITestAdminOptions<T, TProperty> IConfigurable<PropRule, ITestAdminOptions<T, TProperty>>.Configure(Action<PropRule> configurator)
        {
            configurator(rule);
            return this;
        }

        ITestAdminInitial<T, TProperty> IConfigurable<PropRule, ITestAdminInitial<T, TProperty>>.Configure(Action<PropRule> configurator)
        {
            configurator(rule);
            return this;
        }
    }
}